package com.zc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.zc.dao.UserDao;
import com.zc.model.Article;
import com.zc.model.User;
import com.zc.util.HibernateUtil;
import com.zc.util.StringUtil;

public class UserDaoImpl implements UserDao {

	public static void main(String[] args) {
		UserDaoImpl dao = new UserDaoImpl();
		// User user = new User();
		// user.setUsername("burns");
		// user.setPassword("burns");
		// user.setRemark("burns的信息");
		// dao.addUser(user);

		User user1 = new User();
		user1.setUsername("burns");
		user1.setPassword("burns");
		System.out.println(dao.getUser(user1).toString());

		// System.out.println(dao.getAllUser().size());
		// for (User user2 : dao.getAllUser()) {
		// System.out.println(user2.toString());
		// }

	}

	@Override
	public void addUser(User user) {

		Session session = null;

		// Article article = new Article();
		// article.setTitle("2");
		// article.setContex("3");
		// article.setCreator("4");
		// article.setCrateTime("5");
		// article.setLastestEditor("6");
		// article.setLastestEditTime("7");

		// Hibernate的持久化操作
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();// 事务回滚
			System.out.println("数据添加失败");
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();// 关闭Session对象
		}

	}

	@Override
	public User getUser(User user) {
		User tempUser = null;
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			String hql = "from User u where u.username=:username and u.password=:password";
			Query q = session.createQuery(hql);
			q.setParameter("username", user.getUsername());
			q.setParameter("password", user.getPassword());
			List<User> userList = q.list();
			if (userList != null && userList.size() >= 1) {
				tempUser = userList.get(0);
				System.out.println(tempUser.toString());
			}
			session.getTransaction().commit();
			// System.out.println(article.toString());
		} catch (HibernateException e) {
			System.out.println("对象装载失败");
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {

			HibernateUtil.closeSession();
		}
		return tempUser;
	}

	@Override
	public List<User> getAllUser() {
		List<User> userList = new ArrayList<User>();
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			String hql = "from User user";
			Query query = session.createQuery(hql);
			userList = query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtil.closeSession();
		}
		return userList;
	}

	@Override
	public User updateUser(User user) {
		User newUser = null;
		Session session = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			User dbUser = getUser(user);
//			updateObject(dbUser,user);
			if(dbUser!=null){
				String hql = "update User u set u.username=:username and u.password=:password and u.remark=:remark";
				Query q = session.createQuery(hql);
				q.setParameter("username", StringUtil.isEmpty(user.getUsername())==true?dbUser.getUsername():user.getUsername());
				q.setParameter("password", StringUtil.isEmpty(user.getPassword())==true?dbUser.getPassword():user.getPassword());
				q.setParameter("remark", StringUtil.isEmpty(user.getRemark())==true?dbUser.getRemark():user.getRemark());
				q.executeUpdate();
				session.getTransaction().commit();
				newUser = getUser(user);
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtil.closeSession();
		}
		return newUser;
	}

	private User updateObject(User dbUser, User user) {
		User u = null;
		if(dbUser==null&&user!=null){
			u = user;
		}else if(dbUser!=null && user==null){
			u = dbUser;
		}else if(dbUser !=null&&user!=null){
			
			return null;
		}
		return u;
		
	}

	@Override
	public User delteUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

}
